package com.douqu.game.core.config.challenge;

import com.douqu.game.core.config.sprite.MasterParamConfig;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;

/**
 * @author wangzhenfei
 *         2017-11-28 15:28
 *         战斗初始化配置
 */
public class BattleInitConfig extends GameObject{

    /**
     * 战斗中对方的名字显示
     */
    public String masterName;
    /** 主将 */
    public int master;
    /** 主将AI，对应ai表 */
    public int masterAi;
    /** 主将等级配置(对应MasterParam的ID) */
    public int masterLv;
    /** 对应怪物ID */
    public int monsterId;
    /**
     *  怪物等级
     *   1.等级
         2.星级
         3.装备强化等级
         4.装备进阶等级
         5.法宝强化等级
         6.法宝升阶等级
     填为0代表不计算该部分属性
     * */
    public int[] monsterLv;

    /**战斗力**/
    public int fc;

    /**
     * 地图ID
     */
    public int mapId;

    @Override
    public boolean check()
    {
        if(getClass() != WorldBossConfig.class  && (monsterLv == null || monsterLv.length != 14))
        {
            LogUtils.error(getClass().getSimpleName() + " Load BattleInitConfig monsterLv is null! id:" + id);
            return false;
        }

        if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.MAP_KEY, mapId) == null)
        {
            LogUtils.error(getClass().getSimpleName() + " Load BattleInitConfig mapId Error -> id:" + id);
            return false;
        }
        return true;
    }

//    /**
//     * 获取附加属性(目前主要是军团连锁)
//     * @param attrId
//     * @return
//     */
//    public int getExtAddValue(int attrId)
//    {
//        int result = 0;
//
//        LegionChainConfig legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.EQUIP_INTENSIFY.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[6]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.EQUIP_MERGE.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[7]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.HERO_LEVEL.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[8]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.HERO_QUALITY.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[9]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.HERO_STAR.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[10]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.ACCESSORY_INTENSIFY.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[11]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.ACCESSORY_QUALITY.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[12]);
//
//        legionChain = DataFactory.getInstance().getGameObject(DataFactory.LEGION_CHAIN_KEY, E_LegionChinaType.CARD_NUM.getCode());
//        result += legionChain.getAtt(attrId, monsterLv[13]);
//
//        return result;
//    }
//
//    /**
//     * 获取地图类型
//     * @return
//     */
//    public int getMapType()
//    {
//        MapConfig mapConfig = DataFactory.getInstance().getGameObject(DataFactory.MAP_KEY, mapId);
//        return mapConfig.mapType;
//    }

    public MasterParamConfig getMasterParam()
    {
        return DataFactory.getInstance().getGameObject(DataFactory.MASTER_PARAM_KEY, masterLv);
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("monsterId".equals(key))
        {
            monsterId = Integer.parseInt(value);
            if(monsterId != 0)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.MONSTER_KEY, monsterId) == null)
                {
                    LogUtils.error(getClass().getSimpleName() + " BattleInitConfig setVariable Error -> monsterId : " + value + " id:" + id);
                    return false;
                }
            }
            return true;
        }
        else if("monsterLv".equals(key))
        {
            monsterLv = LoadUtils.loadIntArray(key, value);
            if(DataFactory.getInstance().getGameObject(DataFactory.MASTER_PARAM_KEY, masterLv) == null)
            {
                LogUtils.error(getClass().getSimpleName() + " BattleInitConfig setVariable Error -> monsterLv : " + value + " id:" + id);
                return false;
            }
            return true;
        }
        else if("master".equals(key))
        {
            master = Integer.parseInt(value);
            if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.MASTER_KEY, master) == null)
            {
                LogUtils.error(getClass().getSimpleName() + " BattleInitConfig setVariable Error -> master : " + value + " id:" + id);
                return false;
            }
            return true;
        }
        else if("masterAi".equals(key))
        {
            masterAi = Integer.parseInt(value);
            if(masterAi != 0)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.AI_KEY, masterAi) == null)
                {
                    LogUtils.error(getClass().getSimpleName() + " BattleInitConfig setVariable Error -> masterAi:" + value + " id:" + id);
                    return false;
                }
            }
            return true;
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "{" +
                "master=" + master +
                ", masterLv=" + masterLv +
                ", monsterId=" + monsterId +
                ", monsterLv=" + Arrays.toString(monsterLv) +
                "} " + super.toString();
    }
}
